import { ExtOwner, Stockable, Tradable, ExtIntro } from "qqlx-core";

import { isStringNotValid, toNumber, toString } from "@lib/com.verify";

/** 信息介绍 */
export function getIntroUpdater(schema: ExtIntro) {
  return {
    name: toString(schema.name).replace(/\s+/g, ""),
    desc: toString(schema.desc),
    content: toString(schema.content),
    remark: toString(schema.remark),
    sequence: toNumber(schema.sequence),
  };
}

/** 根据 -tradeId- 对数组去重 */
export function filterTradable<T extends Tradable & ExtOwner>(tradables: T[]): T[] {
  const tradedIDs = new Set<string>();

  return tradables.filter((item) => {
    if (isStringNotValid(item.tradeId)) return false;

    if (!tradedIDs.has(item.tradeId)) {
      tradedIDs.add(item.tradeId);
      return true;
    }
    return false;
  });
}

/** 根据 -ivid- 对数组去重 */
export function filterStockable<T extends Stockable>(ivAbles: T[]): T[] {
  const tradedIDs = new Set<string>();

  return ivAbles.filter((item) => {
    if (isStringNotValid(item.ivid)) return false;

    if (!tradedIDs.has(item.ivid)) {
      tradedIDs.add(item.ivid);
      return true;
    }
    return false;
  });
}
